<?php

/**
 * 以时间线的方式把你所读过的书籍展现出来
 * 
 * @package BooksLine
 * @author Jamiexu 
 * @version 1.0
 * @link https://blog.jamiexu.cn
 * email doxm@foxmail.com
 * last time 2020-7-14
 * 
 * 
 * 本插件由Jamiexu单独编写完成，时间线模板使用了百度的HTML源码经过修改成PHP
 */
include_once 'Date.php';

class BooksLine_Plugin implements Typecho_Plugin_Interface
{

    public static function activate()
    {
        self::createTable();
        Helper::addPanel(3, 'BooksLine/manage-booksline.php', "书籍管理", "管理你的书籍时间线吧", 'administrator');
        Helper::addAction('books-action', 'BooksLine_Action');
    }

    public static function deactivate()
    {
        Helper::removePanel(3, 'BooksLine/manage-booksline.php');
    }

    public static function config(Typecho_Widget_Helper_Form $form)
    {
        $name = new Typecho_Widget_Helper_Form_Element_Select('style', [
            'line1' => '样式1',
            'line2' => '样式2',
            'line3' => '样式3',
            'line4' => '样式4',
            'line5' => '样式5',
            'line6' => '样式6',
            'line7' => '样式7',
            'line8' => '样式8',
            'line9' => '样式9',
            'line10' => '样式10',
            'line11' => '样式11',
            'line12' => '样式12',
            'line13' => '样式13',
            'line14' => '样式14',
            'line15' => '样式15',
            'line16' => '样式16',
            'line17' => '样式17',
            'line18' => '样式18'
        ], 'line1', _t('选择时间线样式'));
        $form->addInput($name);
    }

    public static function personalConfig(Typecho_Widget_Helper_Form $form)
    {
    }

    public static function createTable()
    {
        $db = Typecho_Db::get();
        $prefix = $db->getPrefix();
        $sql = "CREATE TABLE IF NOT EXISTS `" . $prefix . "booksline`  (
            `id` int(0) UNSIGNED NOT NULL AUTO_INCREMENT,
            `book_name` varchar(255) NOT NULL,
            `book_author` varchar(255) NOT NULL,
            `book_description` varchar(255) NOT NULL,
            `read_time` varchar(255) NOT NULL,
            PRIMARY KEY (`id`)
          )Engine=MYISAM DEFAULT CHARSET=utf8;";

        $db->query($sql, Typecho_Db::WRITE);
    }

    public static function form($action = '')
    {
        $options = Helper::options();
        $form = new Typecho_Widget_Helper_Form(
            Typecho_Common::url('/action/books-action', $options->index),
            Typecho_Widget_Helper_Form::POST_METHOD
        );

        $name = new Typecho_Widget_Helper_Form_Element_Text('name', NULL, NULL, '书籍名称', '输入书籍的名称');
        $form->addInput($name);

        $author = new Typecho_Widget_Helper_Form_Element_Text('author', NULL, NULL, '编著', '输入书籍编著');
        $form->addInput($author);

        $description = new Typecho_Widget_Helper_Form_Element_Text('description', NULL, NULL, '书籍描述', '输入书籍的描述');
        $form->addInput($description);

        $readtime = new Typecho_Widget_Helper_Form_Element_Date('readtime', NULL, date('Y/m/D', time()), '阅读时间', '输入书籍阅读完成的时间格式：2020/07/30');
        $form->addInput($readtime);

        $submit = new Typecho_Widget_Helper_Form_Element_Submit();
        $form->addInput($submit);

        $id = new Typecho_Widget_Helper_Form_Element_Hidden('id');
        $form->addInput($id);

        $op = new Typecho_Widget_Helper_Form_Element_Hidden('op');
        $form->addInput($op);

        $request = Typecho_Request::getInstance();
        $db = Typecho_Db::get();
        $prefix = $db->getPrefix();
        $book = $db->fetchRow($db->select()->from($prefix . 'booksline')->where('id = ?', $request->id));
        if ($request->id && $request->op == 'up') {
            $submit->value('编辑书籍');
            $name->value($book['book_name']);
            $author->value($book['book_author']);
            $description->value($book['book_description']);
            $readtime->value($book['read_time']);
            $id->value($book['id']);
            $op->value('up');
        } else {
            $submit->value('添加书籍');
            $op->value('in');
        }

        if ($action == 'in' || $action == 'up') {
            $name->addRule('required', _t('必须填写书籍名称'));
            $author->addRule('required', _t('必须填写书籍编著'));
            $description->addRule('required', _t('必须填写书籍描述'));
        }


        return $form;
    }

    public static function loadSources()
    {
        $options = Helper::options();
        _e('<link rel="stylesheet" href="' . Typecho_Common::url('BooksLine/lines/css/jazz-timeline.css', $options->pluginUrl) . '">');
        echo "\r\n";
        echo "\t";
        _e('<script src="' . Typecho_Common::url('BooksLine/lines/js/skrollr.min.js', $options->pluginUrl) . '"></script>');
    }

    public static function render()
    {
        $options = Helper::options();
        $db = Typecho_Db::get();
        $prefix = $db->getPrefix();
        $books = $db->fetchAll(
            $db->select()->from($prefix . 'booksline')->order($prefix . 'booksline.id', Typecho_Db::SORT_DESC)
        );
        _e('<link rel="stylesheet" href="' . Typecho_Common::url('BooksLine/lines/css/jazz-timeline.css', $options->pluginUrl) . '">');
        include_once 'lines/' . $options->plugin('BooksLine')->style . '.php';
        _e('<script src="' . Typecho_Common::url('BooksLine/lines/js/skrollr.min.js', $options->pluginUrl) . '"></script>');
        _e('<script type="text/javascript">
        var s = skrollr.init();
    </script>');
    }
}
